﻿/*

    This file is a part of BinaryDialog.
 
    BiLog IM aka BinaryDialog - WPF Client for XMPP Protocol
    Copyright (C) 2008  Christophe LEMOINE

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;

using BinaryDialog;
using BinaryDialog.Properties;
using BinaryDialog.Core;
using BinaryDialog.Core.UI;
using BinaryDialog.Core.Relationship;
using BinaryDialog.Datas;

namespace BinaryDialog.Windows
{

    public partial class AccountWindow : Window
    {

        private Account account = new Account();

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            bool Glass = false;
            if (Settings.Default.DefaultGlassAuthorization) { Glass = WindowGlassEffect.Set(this, false); }
            if (!Glass) { this.Background = new SolidColorBrush(Settings.Default.DefaultColor); }
        }

        private bool errorid = true;
        private bool errorpassword = true;
        private bool errorport = true;

        public AccountWindow()
        {
            InitializeComponent();
            this.InfoId.Text = this.account.NickName + "@" + this.account.Server;
            this.InfoPort.Text = this.account.Port.ToString();
            this.InfoSSL.IsChecked = this.account.UseSSL;
            this.InfoStartTLS.IsChecked = this.account.UseStartTLS;
            this.InfoScreenName.Text = this.account.ScreenName;
            this.InfoPassword.Password = this.account.Password;
        }

        private void LinkEditAvatar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = false;
            ofd.AutoUpgradeEnabled = true;
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            ofd.RestoreDirectory = true;
            ofd.ShowHelp = false;
            ofd.ShowReadOnly = false;
            ofd.Filter = "Fichier images (*.png, *.jpg, *.jpe, *.jpeg, *.bmp)|*.png;*.jpg;*.jpe;*.jpeg|Portable Network Graphics (*.png)|*.png|Joint Photographic Experts Group (*.jpg, *.jpe, *.jpeg)|*.jpg;*.jpe;*.jpeg|Bitmap (*.bmp)|*.bmp";
            ofd.FilterIndex = 0;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    Uri WantedUri = new Uri("file:///" + ofd.FileName);
                    BitmapDecoder bd = BitmapDecoder.Create(WantedUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                    InfoAvatar.Source = bd.Frames[0];
                    HyperLinkDeleteAvatar.IsEnabled = true;
                }
                catch
                {
                    Uri DefaultUri = new Uri("pack://application:,,,/Icons/user-info.png", UriKind.RelativeOrAbsolute);
                    BitmapDecoder bd = BitmapDecoder.Create(DefaultUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                    InfoAvatar.Source = bd.Frames[0];
                }
            }
        }

        private void LinkDeleteAvatar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Uri DefaultUri = new Uri("pack://application:,,,/Icons/user-info.png", UriKind.RelativeOrAbsolute);
            BitmapDecoder bd = BitmapDecoder.Create(DefaultUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
            InfoAvatar.Source = bd.Frames[0];
            HyperLinkDeleteAvatar.IsEnabled = false;
        }

        private void InfoId_TextChanged(object sender, TextChangedEventArgs e)
        {
            Regex r = new Regex(@"^(([^<>()[\]\\.,;:\s@\""]+"
                + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                + @"[a-zA-Z]{2,}))$");
            this.InfoId.Text = this.InfoId.Text.Trim();
            if (!r.IsMatch(this.InfoId.Text))
            {
                this.InfoId.Foreground = Brushes.Red;
                this.errorid = true;
            }
            else
            {
                this.InfoId.Foreground = Brushes.Black;
                this.errorid = false;
            }
            string[] jid = r.Split(this.InfoId.Text);
            if (jid.Length > 3)
            {
                string last = string.Empty;
                if (jid[2].Length >= 1)
                {
                    last = jid[2].Substring(0, jid[2].Length - 1);
                }
                if (this.InfoScreenName.Text.Trim().Length == 0 || this.InfoScreenName.Text == last)
                {
                    this.InfoScreenName.Text = jid[2];
                }
            }
            ButtonSave.IsEnabled = (!this.errorid && !this.errorport && !this.errorpassword);
        }

        private void InfoPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.InfoPort.Text = this.InfoPort.Text.Trim();
            try
            {
                this.InfoPort.Text = Convert.ToInt32(this.InfoPort.Text).ToString();
                this.InfoPort.Foreground = Brushes.Black;
                this.errorport = false;
            }
            catch
            {
                this.InfoPort.Foreground = Brushes.Red;
                this.errorport = true;
            }
            ButtonSave.IsEnabled = (!this.errorid && !this.errorport && !this.errorpassword);
        }

        private void InfoPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (this.InfoSavePassword.IsChecked.Value && this.InfoPassword.Password.Length < 4)
            {
                this.InfoPassword.Foreground = Brushes.Red;
                this.errorpassword = true;
            }
            else
            {
                this.InfoPassword.Foreground = Brushes.Black;
                this.errorpassword = false;
            }
            ButtonSave.IsEnabled = (!this.errorid && !this.errorport && !this.errorpassword);
        }

        private void InfoSSL_Checked(object sender, RoutedEventArgs e)
        {
            InfoStartTLS.IsChecked = false;
        }

        private void InfoStartTLS_Checked(object sender, RoutedEventArgs e)
        {
            InfoSSL.IsChecked = false;
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void InfoSavePassword_Unchecked(object sender, RoutedEventArgs e)
        {
            this.InfoPassword.Password = string.Empty;
            this.InfoPassword.IsEnabled = false;
            this.errorpassword = false;
        }

        private void InfoSavePassword_Checked(object sender, RoutedEventArgs e)
        {
            this.InfoPassword.IsEnabled = true;
        }

    }

}
